/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: tao
 * Date: 2023-10-30
 * Time: 15:06
 */

public class BfTest {
    public static int bf(String str, String sub) {
        int lenStr = str.length();//主串长度
        int lenSub = sub.length();//子串长度
        if(str == null || sub == null) {
            return -1;
        }
        if(lenStr == 0 || lenSub == 0) {
            return -1;
        }
        int i = 0;//遍历子串的
        int j = 0;//遍历主串的

        while (i < lenStr && j < lenSub) {
            if(str.charAt(i) == sub.charAt(j)) {
                i++;
                j++;
            } else{
                i = i - j + 1;
                j = 0;
            }
        }
        //j遍历完
        if(j >= lenSub) {
            return i - j;
        }
        //i没遍历完，j也没遍历完
        return -1;
    }

    public static void main(String[] args) {
        String str = "ababcabcde";
        String sub = "abcd";
        int ret = bf(str,sub);
        System.out.println(ret);
    }


}
